from django.db import models
from django.contrib.auth.models import User
from schedule.models import Schedule, Offering
# from plan.models import Plan

class DegreeRequirements(models.Model):
    friendly_name = models.CharField(max_length=64)
    is_default = models.BooleanField(default = False)
    courses_per_term = models.IntegerField()
    work_reports = models.IntegerField()
    cse_a = models.IntegerField()
    cse_b = models.IntegerField()
    cse_c = models.IntegerField()
    cse_other = models.IntegerField()
    min_math_sci = models.IntegerField()
    extra_math_sci = models.IntegerField()
    min_engsci_des = models.IntegerField()
    extra_engsci_des = models.IntegerField()
    created_at = models.DateTimeField(auto_now_add = True)
    updated_at = models.DateTimeField(auto_now = True)

    class Meta:
        verbose_name_plural = 'Degree Requirements'
    def __unicode__(self):
        return self.friendly_name

class Student(models.Model):
    uw_id = models.CharField(max_length=12)
    first_name = models.CharField(max_length=250)
    last_name = models.CharField(max_length=250)
    expected_graduation = models.PositiveIntegerField()
    notes = models.TextField(blank=True)
    user = models.ForeignKey(User)
    degree_requirement = models.ForeignKey(DegreeRequirements)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    schedule = models.OneToOneField(Schedule)

    @property
    def full_name(self):
        return u"%s %s" % (self.first_name, self.last_name)

    def __unicode__(self):
        return self.full_name

    def get_term_code(self, term):
        if term in ('1A', 1, '1'):
            term_year = self.expected_graduation - 5
            term_month = 9
        if term in ('1B', 2, '2'):
            term_year = self.expected_graduation - 4
            term_month = 5
        if term in ('2A', 3, '3'):
            term_year = self.expected_graduation - 3
            term_month = 1
        if term in ('2B', 4, '4'):
            term_year = self.expected_graduation - 3
            term_month = 9
        if term in ('3A', 5, '5'):
            term_year = self.expected_graduation - 2
            term_month = 5
        if term in ('3B', 6, '6'):
            term_year = self.expected_graduation - 1
            term_month = 1
        if term in ('4A', 7, '7'):
            term_year = self.expected_graduation - 1
            term_month = 9
        if term in ('4B', 8, '8'):
            term_year = self.expected_graduation
            term_month = 1

        return int('1%s%d' % (str(term_year)[-2:], term_month))

# def add_course_to_schedule(sender, instance, created):
#     if created:
#         planned_course = instance
#         plan = planned_course.plan
#         course = planned_course.course
#         student = plan.student
#         schedule = student.schedule
#         term_id = student.get_term_id(planned_course.term)
#         offerings = Offering.objects.filter(course=course, term=term_id)
#         if len(offerings) == 1:
#             schedule.add(offerings.get())
#             schedule.save()
# 
# post_save.connect(add_course_to_schedule, sender=PlannedCourse)
